﻿$(document).ready(function() {
    var i = 1;
    $("#IsGeneric").click(function() {
        if ($("#IsGeneric").is(':checked')) {
            i = 1;
            $("#AddEmail").click();
            $("#EmailRow").hide();
            $("#AddEmail").show();
            $("#ta_bulk").show();
            $("#tr_bulk_tool").show();
            $("#tr_bulk_tool2").show();
            $("#div_up_bulk").show();
        } else {
            $("#AddEmail").hide();
            $("#EmailRow").show();
            $("#ta_bulk").hide();
            $("#tr_bulk_tool").hide();
            $("#tr_bulk_tool2").hide();
            $("#div_up_bulk").hide();
            $(".editorRow").remove();
        }
    });
    $("#AddEmail").click(function() {
        $.ajax({
            url: this.href,
            cache: false,
            success: function(html) {
                $('#CreateTable > tbody > tr').eq(i).after(html);
                i++;
            }
        });
        return false;
    });
    $("#al_bulk_delim").click(function() {
        i = 1;
        var array = $("#ta_bulk").val().split($("#tb_bulk_delim").val());
        $(".editorRow").remove();
        for (j = 0; j < array.length; j++) {
            $.ajax({
                url: this.href,
                data: ({ email: array[j] }),
                cache: false,
                success: function(html) {
                    $('#CreateTable > tbody > tr').eq(i - 1).after(html);
                    i++;
                }
            });
        }
        return false;
    });
    var success = function (file, server_data, receivedResponse) {   
        var array = jQuery.parseJSON(server_data);  
        $(".editorRow").remove();
        for (z = 0; z < array.length; z++) {
            $.ajax({
                url: "/Account/FilledEditorRow",
                data: ({ email: array[z] }),
                cache: false,
                success: function(html) {
                    $('#CreateTable > tbody > tr').eq(i - 1).after(html);
                    i++;
                }
            });
        }                 
    }; 
    $(function() {
        $("#up_bulk").makeAsyncUploader({
            upload_url: "/Account/AsyncUpload", // Important! This isn't a directory, it's a HANDLER such as an ASP.NET MVC action method, or a PHP file, or a Classic ASP file, or an ASP.NET .ASHX handler. The handler should save the file to disk (or database).
            flash_url: '/Scripts/swfupload.swf',
            button_image_url: '/Resources/blankButton.png',
            upload_success_handler : success,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
        });
    });
});